#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <string.h>

int main(int argc,char *argv[])
{
	if(argc != 3){
		printf("Usage: %s [server:ip] [server:port]\n",argv[0]);
		return 1;
	}

	int sock=socket(AF_INET,SOCK_DGRAM,0);
	if(sock<0){
		perror("sock");
		return 2;
	}
	
	char buf[1024];
	while(1){
		struct sockaddr_in server;
		server.sin_family=AF_INET;
		server.sin_port=htons(atoi(argv[2]));	
		server.sin_addr.s_addr=inet_addr(argv[1]);	

		printf("please enter:");
		ssize_t s=read(0,buf,sizeof(buf)-1);
		if(s>0){
			buf[s]=0;
			sendto(sock,buf,strlen(buf),0,(struct sockaddr*)&server,sizeof(server));
			recvfrom(sock,buf,sizeof(buf)-1,0,NULL,NULL);
			printf("server echo#  %s\n",buf);			
		}
		
	}
	return 0;
}

















